package com.west_traveler.tiyutoutiao.fragment.main_fragment;


import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;

import com.west_traveler.tiyutoutiao.BR;
import com.west_traveler.tiyutoutiao.R;
import com.west_traveler.tiyutoutiao.adapter.CommentRecyclerAdapter;
import com.west_traveler.tiyutoutiao.application.BaseApplication;
import com.west_traveler.tiyutoutiao.databinding.FragmentGameFrgBinding;
import com.west_traveler.tiyutoutiao.entity.game_entity.GameEntity;
import com.west_traveler.tiyutoutiao.entity.recommend_entity.ContestListBean;
import com.west_traveler.tiyutoutiao.interfacess.HttpService;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * A simple {@link Fragment} subclass.
 */
public class GameFrg extends Fragment implements Callback<GameEntity> {
    private FragmentGameFrgBinding binding;
    private CommentRecyclerAdapter<Object> recyclerAdapter;

    public GameFrg() {
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        binding = DataBindingUtil.inflate(inflater, R.layout.fragment_game_frg, container, false);
        return binding.getRoot();
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        HashMap<Class<?>, Pair<Integer,Integer>> map = new HashMap<>();
        map.put(null,Pair.create(R.layout.item_game_not_start, BR.game));

        recyclerAdapter = new CommentRecyclerAdapter<>(getContext(),new ArrayList<>(),map);
        binding.rvGamePageId.setAdapter(recyclerAdapter);

        BaseApplication baseApplication = (BaseApplication) getActivity().getApplication();

        HttpService service = baseApplication.getService();
        service.getGameData().enqueue(this);

    }

    @Override
    public void onResponse(Call<GameEntity> call, Response<GameEntity> response) {
        List<ContestListBean> contest_list = response.body().getData().getContest_list();
        for (int i = 0; i < contest_list.size(); i++) {
        recyclerAdapter.add(i,contest_list.get(i).getContest());
        }

    }

    @Override
    public void onFailure(Call<GameEntity> call, Throwable t) {
        Toast.makeText(getContext(), t.getMessage(), Toast.LENGTH_SHORT).show();
    }
}
